from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
import time
import os

class MouseOperat:
    def __init__(self):
        self.driver=webdriver.Chrome()
        # self.driver.get('https://sahitest.com/demo/clicks.htm')
        self.driver.maximize_window()
        self.ac=ActionChains(self.driver)

    def quit(self,seconds=3):
        time.sleep(seconds)
        self.driver.quit()

    #点击
    def click(self):
        time.sleep(2)
        a=self.driver.find_element(By.XPATH,'//form/input[3]')
        a.click()
        self.ac.click(a).perform()
    #右击
    def context_click(self):
        time.sleep(2)
        b= self.driver.find_element(By.XPATH, '//form/input[4]')
        self.ac.context_click(b).perform()
    #双击
    def double_click(self):
        time.sleep(2)
        c= self.driver.find_element(By.XPATH, '//form/input[2]')
        self.ac.double_click(c).perform()
    # 拖动
    def drag_and_drop(self):
        time.sleep(2)
        self.driver.get("https://sahitest.com/demo/dragDropMooTools.htm")
        d=self.driver.find_element(By.ID,'dragger')
        e=self.driver.find_element(By.XPATH,'//div[3]')
        self.ac.drag_and_drop(d,e).perform()

    def move_to_element(self):
        time.sleep(2)
        self.driver.get('https://sahitest.com/demo/mouseover.htm')
        f=self.driver.find_element(By.XPATH,'//form/input[1]')
        self.ac.move_to_element(f).perform()

    def click_and_hold(self):
        time.sleep(2)
        file ='file:///'+os.path.join(os.path.abspath('html'),'mouse_hold.html')
        self.driver.get(file)
        m=self.driver.find_element(By.ID,'btn1')
        self.ac.click_and_hold(m).perform()
        time.sleep(7)

if __name__ == '__main__':
    mo=MouseOperat()
    # # mo.click()
    # mo.context_click()
    # mo.double_click()
    # mo.drag_and_drop()
    # mo.move_to_element()
    mo.click_and_hold()
    mo.quit()


# os.path.abspath('file1')  #绝对路径显示file1的位置
# os.path.dirname('a') #a文件的上一层






